package com.aracky.system.controller;

import com.aracky.common.util.ConvertUtil;
import com.aracky.common.vo.CommonResult;
import com.aracky.common.vo.PageVO;
import com.aracky.system.model.entity.SysDictData;
import com.aracky.system.model.entity.SysDictType;
import com.aracky.system.model.form.SysDictDataForm;
import com.aracky.system.model.form.SysDictTypeForm;
import com.aracky.system.model.request.SysDictTypeRequest;
import com.aracky.system.model.vo.SysDictDataVO;
import com.aracky.system.model.vo.SysDictTypeVO;
import com.aracky.system.service.SysDictDataService;
import com.aracky.system.service.SysDictTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author zengzhiping
 * @since 2022-07-15 09:11:41
 */
@RestController
@RequestMapping("/sysDictType")
@Api(tags = "字典相关接口")
public class SysDictTypeController {

    @Autowired
    private SysDictTypeService sysDictTypeService;


    @Autowired
    private SysDictDataService sysDictDataService;

    @PostMapping("/save")
    @ApiOperation(value = "新增字典类型")
    public CommonResult saveDictType(@RequestBody SysDictTypeForm sysDictTypeForm) {
        sysDictTypeService.dictCodeExists(sysDictTypeForm);

        SysDictType sysDictType = ConvertUtil.sourceToTarget(sysDictTypeForm, SysDictType.class);
        sysDictTypeService.save(sysDictType);
        return CommonResult.success("字典类型新增成功");
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改字典类型")
    public CommonResult updateDictType(@RequestBody SysDictTypeForm sysDictTypeForm) {
        sysDictTypeService.dictCodeExists(sysDictTypeForm);

        SysDictType sysDictType = ConvertUtil.sourceToTarget(sysDictTypeForm, SysDictType.class);
        sysDictTypeService.updateById(sysDictType);
        return CommonResult.success("字典类型修改成功");
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除字典类型")
    @ApiImplicitParam(name = "dictIds", value = "字典类型ids")
    public CommonResult deleteDictType(Long[] dictIds) {
        List<Long> list=new ArrayList<>();
        for (Long dictId:dictIds){
            list.add(dictId);
        }
        sysDictTypeService.removeBatchByIds(list);
        return CommonResult.success("字典类型删除成功");
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "查询字典类型分页信息")
    public CommonResult<PageVO<SysDictTypeVO>> getDictTypePage(@RequestBody SysDictTypeRequest sysDictTypeRequest) {

        LambdaQueryWrapper<SysDictType> lambdaQueryWrapper = Wrappers.<SysDictType>lambdaQuery()
                .likeRight(StringUtils.isNotBlank(sysDictTypeRequest.getDictCode()), SysDictType::getDictCode, sysDictTypeRequest.getDictCode())
                .likeRight(StringUtils.isNotBlank(sysDictTypeRequest.getDictName()), SysDictType::getDictName, sysDictTypeRequest.getDictName())
                .orderByDesc(SysDictType::getCreateTime);

        Page page = sysDictTypeService.page(sysDictTypeRequest.toPage(), lambdaQueryWrapper);
        PageVO pageVO = new PageVO<>(page);
        List<SysDictTypeVO> sysDictTypeVOList = ConvertUtil.sourceToTargets(page.getRecords(), SysDictTypeVO.class);
        pageVO.setData(sysDictTypeVOList);
        return CommonResult.success(pageVO);
    }


    @PostMapping("data/save")
    @ApiOperation(value = "新增字典数据")
    public CommonResult saveDictData(@RequestBody SysDictDataForm sysDictDataForm) {
        sysDictDataService.dictValueExists(sysDictDataForm);

        SysDictData sysDictData = ConvertUtil.sourceToTarget(sysDictDataForm, SysDictData.class);
        sysDictDataService.save(sysDictData);
        return CommonResult.success("字典数据新增成功");
    }

    @PutMapping("data/update")
    @ApiOperation(value = "修改字典数据")
    public CommonResult updateDictData(@RequestBody SysDictDataForm sysDictDataForm) {
        sysDictDataService.dictValueExists(sysDictDataForm);

        SysDictData sysDictData = ConvertUtil.sourceToTarget(sysDictDataForm, SysDictData.class);
        sysDictDataService.updateById(sysDictData);
        return CommonResult.success("字典数据修改成功");
    }

    @DeleteMapping("data/delete")
    @ApiOperation(value = "删除字典数据")
    @ApiImplicitParam(name = "dataIds", value = "字典数据ids")
    public CommonResult deleteDictData(Long[] dataIds) {
        List<Long> list=new ArrayList<>();
        for (Long dataId:dataIds){
            list.add(dataId);
        }
        sysDictDataService.removeBatchByIds(list);
        return CommonResult.success("字典数据删除成功");
    }


    @GetMapping("/data/{dictId}")
    @ApiOperation(value = "通过字典id查询字典数据数据")
    @ApiImplicitParam(name = "dictId", value = "字典id")
    public CommonResult<List<SysDictDataVO>> getDictData(@PathVariable Long dictId) {
        LambdaQueryWrapper<SysDictData> lambdaQueryWrapper = Wrappers.<SysDictData>lambdaQuery()
                .eq(SysDictData::getDictId,dictId)
//                .in(SysDictData::getDictId, dictId,sysDictType.getDictId())
                .orderByAsc(SysDictData::getDictSort);

        List<SysDictData> dictDataList = sysDictDataService.list(lambdaQueryWrapper);
        List<SysDictDataVO> sysDictDataVOList = ConvertUtil.sourceToTargets(dictDataList, SysDictDataVO.class);

        return CommonResult.success(sysDictDataVOList);
    }


}
